package com.cqc.googleplay.adapter;

import java.util.List;

import com.cqc.googleplay.R;
import com.cqc.googleplay.application.ImageLoaderOptions;
import com.cqc.googleplay.bean.ClassicfyInfoBean;
import com.cqc.googleplay.http.NetUrl;
import com.cqc.googleplay.utils.ToastUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.renderscript.Program.TextureType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ClassifyFragAdapter extends BaseListAdapter<Object> {

	public static final int ViewTypeHead = 0;// 标题
	public static final int ViewTypeInfo = 1;// item info

	public ClassifyFragAdapter(List<Object> list) {
		super(list);
	}

	@Override
	public int getItemViewType(int position) {
		Object object = list.get(position);
		if (object instanceof String) {// 头 标题
			return ViewTypeHead;
		} else {
			return ViewTypeInfo;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		switch (getItemViewType(position)) {
		case ViewTypeHead:
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(), R.layout.item_title_classify_frag, null);
			}
			TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title_classify_frag);
			tv_title.setText("" + list.get(position));
			break;
		case ViewTypeInfo:
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(), R.layout.item_info_classify_frag, null);
				holder = new ViewHolder(convertView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			ClassicfyInfoBean info = (ClassicfyInfoBean) list.get(position);
			// item的第一个
			holder.tv_name1.setText(info.getName1());
			ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + info.getUrl1(), holder.iv1, ImageLoaderOptions.options);
			// item的第2个
			if (TextUtils.isEmpty(info.getName2())) {
				holder.ll2.setVisibility(View.INVISIBLE);
			} else {
				holder.ll2.setVisibility(View.VISIBLE);
				holder.tv_name2.setText(info.getName2());
				ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + info.getUrl2(), holder.iv2, ImageLoaderOptions.options);
			}
			// item的第3个
			if (TextUtils.isEmpty(info.getName3())) {
				holder.ll3.setVisibility(View.INVISIBLE);
			} else {
				holder.ll3.setVisibility(View.VISIBLE);
				holder.tv_name3.setText(info.getName3());
				ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + info.getUrl3(), holder.iv3, ImageLoaderOptions.options);
			}

			initOnclickListener(holder,info);
			break;
		}
		return convertView;
	}

	private void initOnclickListener(ViewHolder holder,final ClassicfyInfoBean info) {
		holder.ll1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ToastUtil.showShortToast(""+info.getName1());
			}
		});
		holder.ll2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ToastUtil.showShortToast(""+info.getName2());
			}
		});
		holder.ll3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ToastUtil.showShortToast(""+info.getName3());
			}
		});
		
	}

	public class ViewHolder {

		public LinearLayout ll1;
		public LinearLayout ll2;
		public LinearLayout ll3;

		public TextView tv_name1;
		public TextView tv_name2;
		public TextView tv_name3;

		public ImageView iv1;
		public ImageView iv2;
		public ImageView iv3;

		public ViewHolder(View itemView) {
			super();
			ll1 = (LinearLayout) itemView.findViewById(R.id.ll1);
			ll2 = (LinearLayout) itemView.findViewById(R.id.ll2);
			ll3 = (LinearLayout) itemView.findViewById(R.id.ll3);

			tv_name1 = (TextView) itemView.findViewById(R.id.tv1_name_item_classify_frag);
			tv_name2 = (TextView) itemView.findViewById(R.id.tv2_name_item_classify_frag);
			tv_name3 = (TextView) itemView.findViewById(R.id.tv3_name_item_classify_frag);

			iv1 = (ImageView) itemView.findViewById(R.id.iv1_item_classify_frag);
			iv2 = (ImageView) itemView.findViewById(R.id.iv2_item_classify_frag);
			iv3 = (ImageView) itemView.findViewById(R.id.iv3_item_classify_frag);
		}

	}
}
